<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Master_ijin extends arfaController {

	public function __construct(){
		parent::__construct();
		$this->load->helper('general');
		$this->cekLogin();
		$this->load->model("m_ijin_model");
	}

	public function index()
	{
		$data["master_ijin"]	= $this->m_ijin_model->get();
		$data["content"]		= $this->load->view("grid_master_ijin",$data,true);
		$data["menu"] 			= "admin";
		$this->load->view('main',$data);
	}

	public function pesan()
	{
		$data["master_ijin"]	= $this->m_ijin_model->get();
		$data["message"] 		= "pesan";
		$data["menu"] 			= "admin";
		$data["content"]		= $this->load->view("grid_master_ijin",$data,true);
		$this->load->view('main',$data);
	}

	public function error()
	{
		$data["master_ijin"]	= $this->m_ijin_model->get();
		$data["message"] 		= "error";
		$data["menu"] 			= "admin";
		$data["content"]		= $this->load->view("grid_master_ijin",$data,true);
		$this->load->view('main',$data);
	}

	public function form($id=FALSE){
		$this->form_validation->set_rules("nama_ijin","Nama Ijin","required");
		$this->form_validation->set_rules("status","STATUS","required");
		if($this->form_validation->run() == FALSE){
			if($id==FALSE){
				$data["id"]			= $id;
				$data["content"]	= $this->load->view("form_master_ijin",$data,true);
				$data["menu"] 		= "admin";
				$this->load->view("main",$data);
			} else {
				$data["id"]			= $id;
				$data["value"]		= $this->m_ijin_model->get($id);
				$data["content"]	= $this->load->view("form_master_ijin",$data,true);
				$data["menu"] 		= "admin";
				$this->load->view("main",$data);
			}
		} else {
			$nama_ijin	= $this->input->post("nama_ijin");
			$keterangan	= $this->input->post("keterangan");
			$status		= $this->input->post("status");

			$cek_nama_ijin = $this->m_ijin_model->get_by("NAMA_IJIN", $nama_ijin, NULL, TRUE);

			if( $cek_nama_ijin == NULL ) {
				if( $id == FALSE ) {
					$data	= array("NAMA_IJIN"=>$nama_ijin,"KETERANGAN"=>$keterangan,"STATUS"=>$status);
					$this->m_ijin_model->save($data);
				} else {
					$data	= array("NAMA_IJIN"=>$nama_ijin,"KETERANGAN"=>$keterangan,"STATUS"=>$status);
					$this->m_ijin_model->save($data, $id);
				}
				redirect("master_ijin/pesan");
			} else {
				if ( $cek_nama_ijin['ID_IJIN'] == $id && $cek_nama_ijin['NAMA_IJIN'] == $nama_ijin ) {
					$data	= array("NAMA_IJIN"=>$nama_ijin,"KETERANGAN"=>$keterangan,"STATUS"=>$status);
					$this->m_ijin_model->save($data, $id);
					redirect("master_ijin/pesan");
				} else {
					redirect("master_ijin/error");
				}
			}
		}
	}
}